<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lokasi extends CI_Controller {

	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		
		if($this->session->userdata('login') === FALSE)
			redirect(base_url());
    }
	
	/*
	 * function index()l
	 * digunakan untuk menampilkan data dalam bentuk table
	 */
	public function index()
	{
		if($this->session->userdata('group') == '1'){
			$this->load->model('model_lokasi');
			
			$q='';
			$page=1;
			$limit=20;
			if(isset($_POST['page'])){
				$page = $_POST['page'];
				$q = $_POST['q'];
			}
			
			$data['lokasi'] = $this->model_lokasi->get_all(($page-1),$limit,$q);
			$data['q'] = $q;
			$data['page'] = $page;
			$data['limit'] = $limit;
			
			$sub_view['sub'] = $this->load->view('admin/lokasi/data',$data,TRUE);
			$view['content'] = $this->load->view('admin/frame',$sub_view,TRUE);
			$this->load->view('index',$view);
		}else{
			redirect(base_url());
		}
	}
	
	/*
	 * function form()
	 * digunakan untuk melakukan tambah dan edit, dengan alur
	   1. memanggil view form
	   2. melakukan proses insert dan update
	 * paramter $id adalah id dari data tsb
	 * jika $id=0, maka ini adalah bendera untuk melakukan tambah data
	 * jika $id!=0, maka ini adalah bendera untuk melakukan edit data
	 */
	public function form($id=0)
	{
		if($this->session->userdata('group') == '1'){
			$this->load->model('model_lokasi');
			$this->load->model('model_jenis_lokasi');
				
			if(isset($_POST['data'])){
				$upload = false;
				if (!empty($_FILES) && $_FILES['userfile']['size'] != 0) {
					$upload = true;
					$this->load->helper('string');
					$config['upload_path'] = "./images/location/";
					$config['allowed_types'] = 'gif|jpg|png|jpeg';
					$config['file_name']	= random_string('alnum', 30);					
					$config['max_size']	= '10240';
					$config['overwrite']  = true;				

					$this->load->library('upload', $config);

					if ( ! $this->upload->do_upload())
					{
						$msg = '{"status":"error","msg":"'.$this->upload->display_errors('','').'"}';
						echo "<script>window.top.respon('$msg');</script>";
						
						return;
					}
					else
					{
						$upload_data = $this->upload->data();
					}
				}
				if($id==0){
					$data = $_POST['data'];
					$data['user_id'] = $this->session->userdata('uid');
					$data['created'] = date('Y-m-d H:i:s');
				
					$url = preg_replace('~[^\\pL0-9_]+~u', '-', str_replace(" ","-",$data['nama']));
					$url = trim($url, "-");
					$url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
					$url = strtolower($url);
					$url = preg_replace('~[^-a-z0-9_]+~', '', $url);

					$data['permalink'] = $url;
					
					if(isset($upload_data)){
						$data['photo'] = $upload_data['orig_name'];
						unset($config);
						$config['image_library'] = 'gd2';
						$config['source_image']	= $upload_data['full_path'];
						//$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = TRUE;
						$config['width']	 = 128;
						$config['height']	= 128;

						$this->load->library('image_lib', $config); 

						if ( ! $this->image_lib->resize())
						{
							echo $this->image_lib->display_errors();
						}
						
					}
					$this->model_lokasi->add($data);
				}else{
					$data = $_POST['data'];
					
					if(isset($upload_data)){
						//menghapus file
						unlink($upload_data['file_path'].$data['photo']);
						
						$data['photo'] = $upload_data['orig_name'];
						
						unset($config);
						$config['image_library'] = 'gd2';
						$config['source_image']	= $upload_data['full_path'];
						$config['maintain_ratio'] = TRUE;
						//$config['create_thumb'] = TRUE;
						$config['width']	 = 128;
						$config['height']	= 128;

						$this->load->library('image_lib', $config); 

						if ( ! $this->image_lib->resize())
						{
							echo $this->image_lib->display_errors();
						}

					}
					$this->model_lokasi->edit($id,$data);
				}
				// /*
				 // * ini digunakan untuk memberitahukan prosesnya berhasil atau tidak
				 // * data ini dalam bentuk json
				 // * status berisikan success atau error
				 // * url berisikan, setelah berhasil, nanti akan diredirect kemana.
				 // */
				$msg = '{"status":"success","url":"admin/lokasi.html"}';
				echo "<script>window.top.respon('$msg');</script>";
				//redirect('admin/lokasi');
				return;
			}
			
			$data['lokasi'] = $this->model_lokasi->get_by($id);
			$data['jenis_lokasi'] = $this->model_jenis_lokasi->get_all(0,20);
			$this->load->view('admin/lokasi/form',$data);
		}else{
			redirect(base_url());
		}
	}
	
	/*
	 * function delete()
	 * digunakan untuk menghapus data
	 * parameter $id adalah id dari data tsb.
	 */
	public function delete($id=0)
	{
		if($this->session->userdata('group') == '1'){
			$this->load->model('model_lokasi');
			
			$this->model_lokasi->delete($id);
			
			redirect('admin/lokasi');
			
		}else{
			redirect(base_url());
		}
	}
	
	function autocomplete(){
		if($this->session->userdata('group') == '1'){
			$this->load->model('model_lokasi');
			
			$q = $_GET['q'];
			
			$lokasi = $this->model_lokasi->get_all(0,10,$q,'yes');

			$output = '';
			foreach($lokasi['data']->result_array() as $row){
				$output .= sprintf("%s|%s\n", $row['nama'].", $row[alamat], $row[nama_kota]",$row['id']);
			}
			$output = substr($output, 0, -1);
			echo $output;
		}
	}
}
